﻿using System.Drawing;
using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;
using com.fukefu.vcode;

namespace com.fukefu.info.b2b.byhand
{
    public class HaoLaBa : Information
    {
        VCODE code = null;
        public override bool Login(string name, string pass, string vcode)
        {
            code = new VCODE(site);
            ParametersData pd = new ParametersData();
            pd.Add("uname", name);
            pd.Add("upwd", pass);
            pd.Add("ckcode", GetFlushCodeImage2());

            string html = source.Post("http://www.haolaba.com/ivlo.html", pd.Read(), "http://www.haolaba.com/login.htm");
            if (html.Equals("1"))
            {
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult info = new InformationResult();
            string html = source.Get("http://www.haolaba.com/Publish.html", "http://www.haolaba.com/Publish.html");
            content = MyFunction.ToText(content);
            //换行需要特殊处理
            content = Regex.Replace(content, "\r\n\r\n", "\r\n\r\n\r\n");
            SourceFormData sfd = new SourceFormData("utf-8");
            //头部数据
            sfd.Write("hidid", site.TypeId[0]);
            sfd.Write("selectedClass", site.TypeId[1]);
            sfd.Write("hidCode", Regex.Match(html, "name=\"hidCode\" value=\"([0-9]+)\"").Groups[1].Value);
            sfd.Write("Title", title);
            sfd.Write("KeyWords", title);
            sfd.Write("content", content);
            sfd.Write("urlname", pif.Get("商务通链接文字"));
            sfd.Write("urlads", pif.Get("商务通链接"));
            sfd.Write("ckurl", pif.Get("商务通链接"));
            sfd.Write("lkqq", pif.Get("QQ"));
            sfd.Write("quqq", pif.Get("QQ"));
            //发送文件
            sfd.WriteFile("MyFile0", RandomPicture.Get());
            sfd.WriteFile("MyFile1", RandomPicture.Get());
            sfd.WriteFile("MyFile1", RandomPicture.Get());
            //尾部数据
            sfd.Write("OldPic", "");
            sfd.Write("DelPic", "");
            sfd.Write("NewPic", "MyFile0,MyFile1,MyFile2");
            sfd.Write("checkCode", GetFlushCodeImage2());
            //结尾
            html = source.Post("http://www.haolaba.com/publish_save.html", sfd.GetContentType(), sfd.Read(), "http://www.haolaba.com/Publish.html");
            if (html.IndexOf("<title>信息发布成功</title>") != -1 || html.Equals(""))
            {
                html = source.Get("http://www.haolaba.com/infomanage_pass_1.html", "http://www.haolaba.com/publish_save.html");
                if (Regex.IsMatch(html, "<a target=_blank href=\"([^\"]+)\">" + title + "</a>"))
                {
                    info.Code = 1;
                    info.Content = "http://www.haolaba.com" + Regex.Match(html, "<a target=_blank href=\"([^\"]+)\">" + title + "</a>").Groups[1].Value;
                }
            }
            else if (html.IndexOf("<tr><td colspan=2 align=center>余额不足!") != -1)
            {
                info.Code = 3;
            }
            else if (Regex.IsMatch(html, "<table border=0><tr><td colspan=2 align=center>([^<]+)"))
            {
                info.Content = Regex.Match(html, "<table border=0><tr><td colspan=2 align=center>([^<]+)").Groups[1].Value;
            }
            return info;
        }

        public override Image GetFlushCodeImage()
        {
            return source.GetImage("http://www.haolaba.com/vcode.html?nocache=" + MyFunction.GetTimestampNow(), "http://www.haolaba.com/login.htm");
        }
        public string GetFlushCodeImage2()
        {
            return code.Get(source.GetStream("http://www.haolaba.com/vcode.html?nocache=" + MyFunction.GetTimestampNow(), "http://www.haolaba.com/login.htm"));
        }
    }
}
